﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using MS.InformationManagement.Presenter.DocumentScheduler;
using MS.InformationManagement.Presenter.Support;

namespace MS.InformationManagement.WP.DocumentCenter.UserDocumentLibrarySchedule
{
    public partial class UserDocumentLibraryScheduleUserControl : BaseControl, IDocumentSchedulerView
    {
        #region [Private Fields]

        private DocumentSchedulerActions _Action;

        #endregion

        #region [Event Handlers]

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnInit(EventArgs e)
        {
            scmngrScripts.Services.Add(new System.Web.UI.ServiceReference
            {
                Path = string.Format("{0}/_vti_bin/MS.InformationManagement.Services/DocumentSchedulerService.asmx",
                    SPContext.Current.Web.Url)
            });
            base.OnInit(e);
            string documentIDQueryString = Request.QueryString.Get("itemsID");

            try
            {
                if (!string.IsNullOrEmpty(documentIDQueryString))
                {
                    string[] documentIDArray = documentIDQueryString.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    this.DocumentIDArray = (from id in documentIDArray
                                            select Convert.ToInt32(id));
                }
                string userLibraryGuid = Request.QueryString.Get("libraryID");
                if (string.IsNullOrEmpty(userLibraryGuid)) { HandleError(new ArgumentException("Por favor verifique si el usuario se encuentra creado")); }
                else
                {
                    string action = Request.QueryString.Get("action");
                    Action = string.IsNullOrEmpty(action) ? DocumentSchedulerActions.View : (DocumentSchedulerActions)Enum.Parse(typeof(DocumentSchedulerActions), action);
                    this.UserLibraryGuid = new Guid(userLibraryGuid);
                    DocumentSchedulerPresenter presenter = new DocumentSchedulerPresenter(this);
                    presenter.HandleLoad();
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region [Public Properties]

        public System.Collections.Generic.IEnumerable<DAL.DocumentCenter.Radicado> Documents
        {
            get
            {
                return rpt_ToScheduleDocuments.DataSource as System.Collections.Generic.IEnumerable<DAL.DocumentCenter.Radicado>;
            }
            set
            {
                rpt_ToScheduleDocuments.DataSource = value;
                rpt_ToScheduleDocuments.DataBind();
            }
        }

        public Guid UserLibraryGuid
        {
            get;
            set;
        }

        public System.Collections.Generic.IEnumerable<int> DocumentIDArray
        {
            get;
            set;
        }

        public new event Presenter.Support.Events.ErrorHandler OnError;

        public event Presenter.Support.Events.SuccessHandler OnSuccess;

        public DocumentSchedulerActions Action
        {
            get { return this._Action; }
            set
            {
                switch (value)
                {
                    case DocumentSchedulerActions.Schedule:
                        rpt_ToScheduleDocuments.Visible = true;
                        break;
                    case DocumentSchedulerActions.View:
                        rpt_ToScheduleDocuments.Visible = false;
                        break;

                }
                this._Action = value;
            }
        }

        #endregion
    }
}
